﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLogic;
using Common;
using Model;
using System.Web.Security;


namespace AppAdmin.Controllers
{
    public class UserInfoController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Form()
        {
            return View();
        }

        [HttpGet]
        public ActionResult GetGridJson(Pagination pagination, string keyword)
        {
            int Total = 0;
            var list = new UserInfoLogic().getPageDate<T_Sys_AdminUser, int>(c => new { c.UserId, c.LoginName, c.RealName, c.Phone, c.State, c.LoginIP, c.LoginTime, c.CreateUser, c.CreateTime, c.Address, c.Sex }, c => c.IsDelete == false, c => c.UserId, pagination.page, pagination.rows, out Total);
            pagination.records = Total;
            var data = new
            {
                rows = list,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        //[HttpGet]
        //public ActionResult GetSelectJson(string enCode)
        //{
        //    var data = new UserInfoLogic().GetItemList(enCode);
        //    List<object> list = new List<object>();
        //    foreach (Sys_ItemsDetail item in data)
        //    {
        //        list.Add(new { id = item.F_ItemCode, text = item.F_ItemName });
        //    }
        //    return Content(list.ToJson());
        //}
        [HttpGet]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = new UserInfoLogic().GetForm(Convert.ToInt32(keyValue));
            return Content(data.ToJson());
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(FormCollection collection,  string keyValue)
        {
            T_Sys_AdminUser model=new T_Sys_AdminUser ();
            UserInfoLogic bll = new UserInfoLogic();
            model.LoginName=collection["LoginName"];
            model.Password=collection["Password"].Trim();
            model.RealName=collection["RealName"];
            model.Phone=collection["Phone"];
            model.Address=collection["Address"];
            model.State = Convert.ToInt32(collection["Hd_State"]) == 0 ? false : true;
            model.Address=collection["Address"];
            model.Sex = Convert.ToInt32(collection["Hd_Sex"]) == 0 ? false : true; 

             if (!string.IsNullOrEmpty(keyValue))//修改
             {
                string pwd= FormsAuthentication.HashPasswordForStoringInConfigFile(FormsAuthentication.HashPasswordForStoringInConfigFile(model.Password, "MD5").ToLower(), "MD5");
                 model.UserId = Convert.ToInt32(keyValue);
                 bll.UpdateInfo(model, pwd);
             }
             else
             {
                 model.CreateUser = ThisAdminSession.LoginName;
                 model.CreateTime = DateTime.Now;
                 bll.AddInfo(model);
             }
            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            new UserInfoLogic().DelInfo(Convert.ToInt32(keyValue));
            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "删除成功。" }.ToJson());
        }
        [HttpPost]
        public ActionResult Disabled(string keyValue)
        {
            new UserInfoLogic().LockInfo(Convert.ToInt32(keyValue));
            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
        }
        [HttpPost]
        public ActionResult Enabled(string keyValue)
        {
            new UserInfoLogic().UnLockInfo(Convert.ToInt32(keyValue));
            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
        }
    }
}
